<?php
/**
 * Description of HTech_Report
 *
 * @author andres
 */
abstract class HTech_Report implements HTech_Interface_Report
{
    protected $_name;
    protected $_form;
    protected $_sql;
    protected $_headers;
    protected $_index;
    protected $_params;
    
    public function __construct() 
    {
        $this->_setName();
        $this->_setForm();
        $this->_setSql();
        $this->_setHeaders();
        $this->_setIndex();
    }
    
    abstract protected function _setName();
    abstract protected function _setForm();
    abstract protected function _setSql();
    abstract protected function _setHeaders();
    abstract protected function _setIndex();
    
    abstract public function setParams($params);
    
    public function fetch()
    {
        if(!$this->_params) 
            throw new Exception($this->translate("Report params not set"));
        
        $db = Zend_Registry::get('db');
        
        if(count($this->_sql) == 1) {
            $stmt = $db->query($this->_sql,$this->_params);
            return $stmt->fetchAll();            
        } else {        
            $data = array();
            $index = $this->_index;
            foreach($this->_sql as $sql) {
                $stmt = $db->query($sql,$this->_params);
                foreach($stmt->fetchAll() as $row) {
                    $r = $row->$index;
                    if(!isset($data[$r])) $data[$r] = new stdClass();
                    foreach($row as $key => $val) $data[$r]->$key = $val;
                }
            }
            return $this->_process($data);
        }
    }
    
    protected function _process($data)
    {
        return $data;
    }
    
    public function getName()
    {
        return $this->_name;
    }
    
    public function getForm()
    {
        return $this->_form;
    }
    
    public function getHeaders($columns = array())
    {
        if(count($columns) == 0)
            return $this->_headers;
        $headers = new stdClass();
        foreach($columns as $col)
            $headers->$col = $this->_headers->$col;
        return $headers;
    }
    
    public function getFootnotes() {
        return array();
        /*
        return array(
            $this->translate('Notes'),
            $this->translate('Note 1'),
            $this->translate('Note 2'),
            $this->translate('Etc'),
            ''
        );
         * 
         */
    }
    
    public function getHeadings() {
        return array();
        /*
        return array(
            '',
            $this->translate('Heading title'),
            $this->translate('Subtitle'),
            $this->translate('Etc')
        );
         * 
         */
    }
    
    public function getFilename($post = array())
    {
        $ts = date("YmdHis"); 
        if(isset($post['Date']))
            return $post['Report'].'_'.$post['Date'].'_TS'. $ts; 
        if(isset($post['DateFrom'])&&isset($post['DateTo']))
            return $post['Report'].'_From_'.$post['DateFrom'].'_To_'.$post['DateTo'].'_TS'. $ts; 
        return isset($post['Report']) ? $post['Report'].'_TS'.$ts : 'REPORT_TS'.$ts;
    }
    
    public function translate($value)
    {            
        return Zend_Registry::get('Zend_Translate')->_($value);
    }

}
